package user

import (
	"strings"

	"gddgame.cc/galaxy/common/dsl/game/reward"
	"gddgame.cc/galaxy/core/models"
	"gddgame.cc/galaxy/utils"
)

//go:generate msgp

type GameType byte

// 账号信息，等级信息，装备信息，道具信息，属性值
//msgp:tuple GameUser
type GameUser struct {
	UserId   string `json:"user_id" msg:"user_id"`
	ShowId   string `json:"id" msg:"id"`
	NickName string `json:"nick_name" msg:"nick_name"`
	Avatar   string `json:"avatar" msg:"avatar"`
	LastTime string `json:"last_time" msg:"last_time"`

	Extend interface{} `json:"extend" msg:"extend"`
}

func (user *GameUser) BindOpen(base UserBase, userOpen *models.UserOpen) {
	user.UserId = userOpen.Id
	user.ShowId = utils.ToString(base.GetId())
	user.Avatar = userOpen.Avatar
	user.NickName = userOpen.NickName
	user.LastTime = ""
	if userOpen.LastTime != nil {
		user.LastTime = userOpen.LastTime.String()
	}
}

//msgp:tuple GameOtherUser
type GameOtherUser struct {
	UserId   string `json:"user_id" msg:"user_id"`
	ShowId   string `json:"id" msg:"id"`
	NickName string `json:"nick_name" msg:"nick_name"`
	Avatar   string `json:"avatar" msg:"avatar"`
	LastTime string `json:"last_time" msg:"last_time"`

	Online     bool `json:"online" msg:"online"`
	RoomStatus bool `json:"room_status" msg:"room_status"`
	TeamStatus bool `json:"team_status" msg:"team_status"`
	IsFriend   bool `json:"is_friend" msg:"is_friend"`

	Extend interface{} `json:"extend" msg:"extend"`
}

func (user *GameOtherUser) BindOpen(base UserBase, userOpen *models.UserOpen) {
	user.UserId = userOpen.Id
	user.ShowId = utils.ToString(base.GetId())
	user.Avatar = userOpen.Avatar
	user.NickName = userOpen.NickName
	user.LastTime = ""
	if userOpen.LastTime != nil {
		user.LastTime = userOpen.LastTime.String()
	}
}

type MessageType byte

//msgp:tuple UserMessageExt
type UserMessageExt struct {
	Id       int           `json:"id" msg:"id"`
	Type     MessageType   `json:"type" msg:"type"`
	TargetId string        `json:"target_id" msg:"target_id"`
	Status   bool          `json:"status" msg:"status"`
	Title    string        `json:"title" msg:"title"`
	Reward   reward.Reward `json:"reward" msg:"reward"`
}

func NewUserMessage(message *UserMessage, language string, templates map[MessageType]map[string]string) *UserMessageExt {
	title := templates[message.Type][language]
	if message.Content != nil && len(message.Content) > 0 {
		for key, value := range message.Content {
			title = strings.Replace(title, `{`+key+`}`, value, 1)
		}
	}
	ext := &UserMessageExt{
		Id:       message.Id,
		Type:     message.Type,
		TargetId: message.TargetId,
		Status:   message.Status,
		Title:    title,
		Reward:   message.Reward,
	}

	return ext
}

//msgp:tuple RankUser
type RankUser struct {
	Rank           int `json:"rank" msg:"rank"`
	*GameOtherUser `json:"user" msg:"user"`
	Score          int `json:"score" msg:"score"`
}
